package oop.ex4.crosswords;

/**
 * A simple implementation of the CrosswordEntry interface. <br>
 * The definition is not stored locally but rather a pointer to the dictionary is saved.
 * This saves memory and support on-the-fly changes in the dictionary.
 * @author yomach
 * @author mbattat
 */
public class MyCrosswordEntry implements CrosswordEntry {
	
	///////////////////////////////////////////////////////
	//////////////////////Data Members/////////////////////
	///////////////////////////////////////////////////////
	
	/**
	 * The dictionary.
	 */
	protected CrosswordDictionary _dict;
	
	/**
	 * The term.
	 */
	protected String _term;
	
	/**
	 * The position.
	 */
	protected CrosswordPosition _position;
	
	///////////////////////////////////////////////////////
	//////////////////////Constructors/////////////////////
	///////////////////////////////////////////////////////
	
	/**
	 * A constructor that creates a new Entry with the given parameters.
	 * @param term The term for the entry.
	 * @param dict The dictionary from which the definition is to be taken.
	 * @param position The position of the entry.
	 */
	public MyCrosswordEntry(String term, CrosswordDictionary dict, CrosswordPosition position) {
		_term = term;
		_dict = dict;
		_position = position;
	}
	
	/**
	 * A constructor that creates a new empty Entry with the given parameters.
	 * @param dict The dictionary from which the definition is to be taken.
	 * @param position The position of the entry.
	 */
	public MyCrosswordEntry(CrosswordDictionary dict, CrosswordPosition position) {
		this(null, dict, position);
	}
	
	///////////////////////////////////////////////////////
	/////////////////////Public Methods////////////////////
	///////////////////////////////////////////////////////
	

	@Override
	public String getDefinition() {
		return _dict.getTermDefinition(_term);
	}

	@Override
	public int getLength() {
		if (_term == null) { return 0; }
		return _term.length();
	}

	@Override
	public CrosswordPosition getPosition() {
		return _position;
	}
	
	/**
	 * {@inheritDoc} <br>
	 * An empty entry returns null.
	 */
	@Override
	public String getTerm() {
		return _term;
	}
	
	@Override
	public String toString() {
		return _position + " - " + _term + ":" + _dict.getTermDefinition(_term);
	}
	
	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof CrosswordEntry)) { return false; }
		CrosswordEntry entry = (CrosswordEntry) obj;
		return this._position.equals(entry.getPosition());
	}
}
